/**
 * @author 0283000115
 * @date 2018年3月3日-下午2:32:37
 */
package com.zte.claa.inficombo.csa.app.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zte.claa.inficombo.csa.app.controller.ret.ApiResult;
import com.zte.claa.inficombo.csa.app.controller.ret.AppInfo;
import com.zte.claa.inficombo.csa.app.controller.ret.AppStatus;
import com.zte.claa.inficombo.csa.app.dao.mysql.impl.ProjCustAttrMapper;
import com.zte.claa.inficombo.csa.app.model.app.AppTarget;
import com.zte.claa.inficombo.csa.app.model.dev.DevPagePara;
import com.zte.claa.inficombo.csa.app.model.dev.SensorDevEuiPV;
import com.zte.claa.inficombo.csa.app.model.proj.ProjCustAttrBean;
import com.zte.claa.inficombo.csa.app.service.correction.LocCorrectionService;
import com.zte.claa.inficombo.csa.app.service.csa.CSAMainService;
import com.zte.claa.inficombo.csa.app.service.custparam.LocCustParamModelService;
import com.zte.claa.inficombo.csa.app.service.dev.DevBaseService;
import com.zte.claa.inficombo.csa.app.service.jsplugin.CSAJSPluginService;
import com.zte.claa.inficombo.csa.app.service.mqttproxy.IMqttProxyService;
import com.zte.claa.inficombo.csa.app.service.rptparse.ext.gnss.HCGNSSParserService;
import com.zte.claa.inficombo.csa.app.service.rptparse.ext.rawtransfer.DefaultRawTransferService;

import io.swagger.annotations.ApiOperation;

/**
 * @author 0283000115
 * 
 * 内部管理接口，不需要token验证。但需要内部固定的token口令》token=Claa2017
 */

@RestController 
@RequestMapping("/csa/admin") 
public class CSAAdminController extends BaseController {
	// private static final SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final String CSA_ADMIN_TOKEN = "Claa2017";
	// logger
	private Logger logger = LoggerFactory.getLogger(CSAAdminController.class);
	
	@Autowired 
	private CSAMainService csaMainService;
	
    @Autowired 
    private CSAJSPluginService csaJsPluginService;
    
    @Autowired 
    private DevBaseService devBaseService;
    
    @Autowired 
    private HCGNSSParserService hcgnssService;
    
    @Autowired
	private ProjCustAttrMapper custAttrMapper;
    
    @Autowired
    private DefaultRawTransferService rawTransferService;
    
    @Autowired
    private  LocCorrectionService locCorrectionService;
    
    @Autowired
    private LocCustParamModelService locCustParamModelService;
    
    @Autowired
    private IMqttProxyService mqttProxyService;
    
    /**
     * 查询CSA中设置的AppEUI列表及到CSIF的连接状态.
     * 
     * @param request
     * @return
     * @throws Exception
     */
	@ApiOperation(value = "查询CSA中设置的AppEUI列表", notes = "")
	@RequestMapping(value={"/appeuilist"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> appEuiList(HttpServletRequest request,
			@RequestParam("token") String token) throws Exception
	{	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		ApiResult<List<AppInfo>> ret = new ApiResult<List<AppInfo>>();
		List<AppInfo> retData = new ArrayList<AppInfo>();
		List<String> appeuiList = this.csaMainService.getAppEuis();
		for(String appeui : appeuiList){
			AppTarget apptg = this.csaMainService.getAppTarget(appeui);
			AppStatus appst = this.csaMainService.getAppLinkState(appeui);
			if(apptg == null){
				apptg = new AppTarget();
				apptg.setAppEUI(appeui);
			}
			AppInfo app = new AppInfo();
			app.setAppAuthKey(apptg.getAppAuthKey());
			app.setAppEUI(apptg.getAppEUI());
			app.setCsifIpAddr(apptg.getCsifIpAddr());
			app.setCsifPort(apptg.getCsifPort());
			app.setLinkStatus(appst.getLinkStatus());
			retData.add(app);
		}
		ret.setData(retData);
		ret.setRemark("success");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
	
	/**
     * 停止某个appeui对应的链路.
     * 
     * @param request
     * @return
     * @throws Exception
     */
	@ApiOperation(value = "停止某个appeui对应的链路", notes = "")
	@RequestMapping(value={"/appeuistop"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> appeuiStop(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("appeui") String appEui) throws Exception
	{	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		appEui = appEui.trim();
		ApiResult<String> ret = new ApiResult<String>();
		String rsp = this.csaMainService.CSQuit(appEui);
		logger.info("appeuiStop: appeui="+appEui+", rsp="+rsp);
		ret.setData(rsp);
		ret.setRemark("finish");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
	
	/**
     * 认证某个appeui.
     * 
     * @param request
     * @return
     * @throws Exception
     */
	@ApiOperation(value = "认证某个appeui对应的链路", notes = "")
	@RequestMapping(value={"/appeuiauth"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> appeuiAuth(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("appeui") String appEui) throws Exception
	{	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		appEui = appEui.trim();
		ApiResult<String> ret = new ApiResult<String>();
		String rsp = this.csaMainService.CSJoin(appEui);
		logger.info("appeuiAuth: appeui="+appEui+", rsp="+rsp);
		ret.setData(rsp);
		ret.setRemark("finish");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
	
	/**
     * 重新加载Js插件.
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "重新加载Js插件", notes = "")
    @RequestMapping(value={"/refreshjsplugin"}, method = RequestMethod.GET)
    @ResponseBody
    public ApiResult<?> refreshJsPlugin(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("devtype") String devtype) throws Exception
    {    
        if(!CSA_ADMIN_TOKEN.equals(token)){
            ApiResult<String> ret1 = new ApiResult<String>();
            ret1.setData("");
            ret1.setRemark("Token is invalid!");
            ret1.setResult(ApiResult.RET_INVALID_TOKEN);
            return ret1;
        }
        ApiResult<String> ret = new ApiResult<String>();
        devtype = devtype.trim();
        try
        {
            this.csaJsPluginService.refreshJsPLugin(devtype);  
            ret.setRemark("refresh "+ devtype +" js plugin successfully");   
            ret.setResult(ApiResult.RET_SUCC);            
            logger.info("refresh "+ devtype +" js plugin successfully");            
        }catch(Exception e)
        {
            ret.setRemark(e.getMessage());
            ret.setResult(ApiResult.RET_FAIL);              
            logger.error("refresh "+ devtype +" js plugin failed");
        }
        return ret;
    }
    
    
    /**
     * 重新加载gps纠偏配置信息
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "重新加载gps纠偏配置信息", notes = "")
    @RequestMapping(value={"/reloadcorrectconfig"}, method = RequestMethod.GET)
    @ResponseBody
    public ApiResult<?> reloadCorrectConfig(HttpServletRequest request,
			@RequestParam("token") String token) throws Exception
    {    
        if(!CSA_ADMIN_TOKEN.equals(token)){
            ApiResult<String> ret1 = new ApiResult<String>();
            ret1.setData("");
            ret1.setRemark("Token is invalid!");
            ret1.setResult(ApiResult.RET_INVALID_TOKEN);
            return ret1;
        }
        ApiResult<String> ret = new ApiResult<String>();
        try
        {
            this.locCorrectionService.reload();  
            ret.setRemark("reload Correction gps config successfully");   
            ret.setResult(ApiResult.RET_SUCC);            
            logger.info("reload Correction gps config successfully");            
        }catch(Exception e)
        {
            ret.setRemark(e.getMessage());
            ret.setResult(ApiResult.RET_FAIL);              
            ret.setRemark("reload Correction gps config failed");   
        }
        return ret;
    }
    
    
    /**
     * 重新加载gps纠偏配置信息
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "重新加载定位全局配置信息", notes = "")
    @RequestMapping(value={"/reloadLocationconfig"}, method = RequestMethod.GET)
    @ResponseBody
    public ApiResult<?> reloadLocationConfig(HttpServletRequest request,
			@RequestParam("token") String token) throws Exception
    {    
        if(!CSA_ADMIN_TOKEN.equals(token)){
            ApiResult<String> ret1 = new ApiResult<String>();
            ret1.setData("");
            ret1.setRemark("Token is invalid!");
            ret1.setResult(ApiResult.RET_INVALID_TOKEN);
            return ret1;
        }
        ApiResult<String> ret = new ApiResult<String>();
        try
        {
            this.locCustParamModelService.reloadLocCustParam(); 
            ret.setRemark("reload Location config successfully");   
            ret.setResult(ApiResult.RET_SUCC);            
            logger.info("reload Location config successfully");            
        }catch(Exception e)
        {
            ret.setRemark(e.getMessage());
            ret.setResult(ApiResult.RET_FAIL);              
            ret.setRemark("reload Location config failed");   
        }
        return ret;
    }
    
    
    /**
     * gps纠偏配置开关开关
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "打开/关闭gps纠偏开关:on->打开,off->关闭", notes = "")
    @RequestMapping(value={"/switchgpscorrect"}, method = RequestMethod.GET)
    @ResponseBody
    public ApiResult<?> switchGpsCorrect(HttpServletRequest request,@RequestParam("token") String token, 
    		@RequestParam("switch") String sw) throws Exception
    {    
        if(!CSA_ADMIN_TOKEN.equals(token)){
            ApiResult<String> ret1 = new ApiResult<String>();
            ret1.setData("");
            ret1.setRemark("Token is invalid!");
            ret1.setResult(ApiResult.RET_INVALID_TOKEN);
            return ret1;
        }
        ApiResult<String> ret = new ApiResult<String>();
        try
        {
            this.locCorrectionService.switchCorrection(sw);
            ret.setRemark("switch gps correction successfully");   
            ret.setResult(ApiResult.RET_SUCC);            
            logger.info("switch gps correction successfully");            
        }catch(Exception e)
        {
            ret.setRemark(e.getMessage());
            ret.setResult(ApiResult.RET_FAIL);              
            ret.setRemark("switch gps correction failed");   
        }
        return ret;
    }
    
    
    
    /**
     * gps纠偏配置开关开关
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "打开/关闭gw的lora信号强度rssi开关:on->打开,off->关闭", notes = "")
    @RequestMapping(value={"/switchgwrssipublish"}, method = RequestMethod.GET)
    @ResponseBody
    public ApiResult<?> switchGwRssiPublish(HttpServletRequest request,@RequestParam("token") String token, 
    		@RequestParam("switch") String sw) throws Exception
    {    
        if(!CSA_ADMIN_TOKEN.equals(token)){
            ApiResult<String> ret1 = new ApiResult<String>();
            ret1.setData("");
            ret1.setRemark("Token is invalid!");
            ret1.setResult(ApiResult.RET_INVALID_TOKEN);
            return ret1;
        }
        ApiResult<String> ret = new ApiResult<String>();
        try
        {
            this.locCorrectionService.switchGwRssiPubllish(sw);
            ret.setRemark("switch gateway rssi publish successfully");   
            ret.setResult(ApiResult.RET_SUCC);            
            logger.info("switch gateway rssi publish successfully");            
        }catch(Exception e)
        {
            ret.setRemark(e.getMessage());
            ret.setResult(ApiResult.RET_FAIL);              
            ret.setRemark("switch gw rssi publish failed");   
        }
        return ret;
    }
    
    
    
    /**
     * sensor---dtu映射关系查询.
     * 
     * @param request  pageindex = 1,2,3,....
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "sensor-dtu映射关系列表查询", notes = "pageindex=[1,n]")
	@RequestMapping(value={"/sensorlist"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> sensorlist(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("pageindex") String pageIndex) throws Exception
    {	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		pageIndex = pageIndex.trim();
		logger.info("sensorlist: pageIndex="+pageIndex);
		DevPagePara para = new DevPagePara();
		para.setPageIndex(Integer.parseInt(pageIndex));
		List<SensorDevEuiPV> pvList = this.devBaseService.querySensorDevEuiPVs(para);		
		ApiResult<List<SensorDevEuiPV>> ret = new ApiResult<List<SensorDevEuiPV>>();		
		ret.setData(pvList);
		ret.setRemark("finish");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
    
    /**
     * sensor--dtu映射关系查询.
     * 
     * @param request  
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "单个sensor-dtu映射关系查询", notes = "sensorid=xxx")
	@RequestMapping(value={"/sensorqry"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> sensorqry(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("sensorid") String sensorId) throws Exception
    {	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		sensorId = sensorId.trim();
		logger.info("sensorqry: sensorId="+sensorId);
		SensorDevEuiPV pv = this.devBaseService.querySensorDevEuiPV(sensorId);		
		ApiResult<SensorDevEuiPV> ret = new ApiResult<SensorDevEuiPV>();		
		ret.setData(pv);
		ret.setRemark("finish");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
	
    /**
     * sensor--dtu映射关系添加.
     * 
     * @param request  
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "sensor-dtu映射关系添加", notes = "deveui=xxx&sensorid=aa_bb_cc")
	@RequestMapping(value={"/sensoradd"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> sensoradd(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("deveui") String devEui, 
			@RequestParam("sensorid") String sensorIds,
			@RequestParam("hostip") String hostIp, 
			@RequestParam("hostport") int hostPort, 
			@RequestParam("remark") String remark ) throws Exception
    {	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		devEui = devEui.trim();
		sensorIds = sensorIds.trim();
		hostIp = hostIp.trim();
		logger.info("sensoradd: deveui="+devEui+", sensorid="+sensorIds);
		String[] sensorIdArr = sensorIds.split("_");
		for(String sensorId : sensorIdArr){
			if(sensorId.trim().equals("")){
				continue;
			}
			SensorDevEuiPV pv = new SensorDevEuiPV();
			pv.setDeveui(devEui);
			pv.setSensorid(sensorId);
			pv.setHostip(hostIp);
			pv.setHostport(hostPort);
			pv.setRemark(remark);
			this.devBaseService.remvoeSensorDevEuiPV(sensorId);
			this.devBaseService.addSensorDevEuiPV(pv);
			this.hcgnssService.addSensorDevEuiPV(pv);
		}	
		ApiResult<String> ret = new ApiResult<String>();		
		ret.setData("OK");
		ret.setRemark("finish");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
    
    /**
     * sensor--dtu映射关系删除.
     * 
     * @param request  
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "单个sensor-dtu映射关系删除", notes = "sensorid=xxx")
	@RequestMapping(value={"/sensordel"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> sensordel(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("sensorid") String sensorId) throws Exception
    {	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		sensorId = sensorId.trim();
		logger.info("sensordel: sensorId="+sensorId);
		int retcode = this.devBaseService.remvoeSensorDevEuiPV(sensorId);		
		ApiResult<String> ret = new ApiResult<String>();		
		ret.setData("删除记录数："+retcode);
		ret.setRemark("finish");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
    
    /**
     * deveui对应的host连接关闭.
     * 
     * @param request  
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "关闭deveui对应的gnss-host连接", notes = "deveui=xxx")
	@RequestMapping(value={"/closegnsshostconn"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> closegnsshostconn(HttpServletRequest request, 
			@RequestParam("token") String token, 
			@RequestParam("deveui") String devEui) throws Exception
    {	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		devEui = devEui.trim();
		logger.info("closegnsshostconn: deveui="+devEui);
		String msg = this.hcgnssService.releaseGnssHostConnByDevEui(devEui);		
		ApiResult<String> ret = new ApiResult<String>();		
		ret.setData(msg);
		ret.setRemark("finish");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
    
    /**
     * 透传服务器地址添加.
     * 
     * @param request  
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "透传服务器地址添加", notes = "svrip=xxx&svrport=aa_bb_cc")
	@RequestMapping(value={"/rawdatadestsvradd"}, method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<?> rawdatadestsvradd(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("destsvrip") String destsvrip, 
			@RequestParam("destsvrport") int destsvrport,
			@RequestParam("projectid") String projectid) throws Exception 
    {	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		destsvrip = destsvrip.trim();
		projectid = projectid.trim();
		logger.info("rawdatadestsvradd: destsvrip="+destsvrip+", destsvrport="+destsvrport);
		// 添加记录: IP, 首先删除已有记录.
		this.custAttrMapper.removeProjCustAttrBean(projectid, DefaultRawTransferService.RAW_DEST_SVRIP);
		ProjCustAttrBean pcab = new ProjCustAttrBean();
		pcab.setAttrname(DefaultRawTransferService.RAW_DEST_SVRIP);
		pcab.setAttrvalue(destsvrip);
		pcab.setProjectid(projectid);
		pcab.setStatus(ProjCustAttrBean.STATUS_VALID);
		pcab.setRemark(new Date().toString());
		this.custAttrMapper.addProjCustAttrBean(pcab);
		// 添加记录: port
		this.custAttrMapper.removeProjCustAttrBean(projectid, DefaultRawTransferService.RAW_DEST_SVRPORT);
		ProjCustAttrBean pcab2 = new ProjCustAttrBean();
		pcab2.setAttrname(DefaultRawTransferService.RAW_DEST_SVRPORT);
		pcab2.setAttrvalue(String.valueOf(destsvrport));
		pcab2.setProjectid(projectid);
		pcab2.setStatus(ProjCustAttrBean.STATUS_VALID);
		pcab2.setRemark(new Date().toString());
		this.custAttrMapper.addProjCustAttrBean(pcab2);
		this.rawTransferService.addNewDestSvr(projectid, destsvrip, destsvrport);
		// 结果返回
		ApiResult<String> ret = new ApiResult<String>();		
		ret.setData(projectid);
		ret.setRemark("Add OK");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
    
    /**
     * 透传服务器地址删除.
     * 
     * @param request  
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "透传服务器地址删除", notes = "projectid=xxx")
   	@RequestMapping(value={"/rawdatadestsvrremove"}, method = RequestMethod.GET)
   	@ResponseBody
	public ApiResult<?> rawdatadestsvrremove(HttpServletRequest request,
			@RequestParam("token") String token, 
			@RequestParam("projectid") String projectid) throws Exception 
    {	
		if(!CSA_ADMIN_TOKEN.equals(token)){
			ApiResult<String> ret1 = new ApiResult<String>();
			ret1.setData("");
			ret1.setRemark("Token is invalid!");
			ret1.setResult(ApiResult.RET_INVALID_TOKEN);
			return ret1;
		}
		projectid = projectid.trim();
		logger.info("rawdatadestsvrremove: projectid="+projectid);
		this.custAttrMapper.removeProjCustAttrBean(projectid, DefaultRawTransferService.RAW_DEST_SVRIP);
		this.custAttrMapper.removeProjCustAttrBean(projectid, DefaultRawTransferService.RAW_DEST_SVRPORT);
		this.rawTransferService.removeDestSvr(projectid);
		// 结果返回
		ApiResult<String> ret = new ApiResult<String>();		
		ret.setData(projectid);
		ret.setRemark("Remove OK");
		ret.setResult(ApiResult.RET_SUCC);		
		return ret;
	}
    
    /**
     * MQTTProxy服务重启.
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "重新初始化MQTTProxy服务", notes = "")
    @RequestMapping(value={"/reinitmqttproxyserivce"}, method = RequestMethod.GET)
    @ResponseBody
    public ApiResult<?> reinitmqttproxyserivce(HttpServletRequest request,
			@RequestParam("token") String token) throws Exception
    {    
        if(!CSA_ADMIN_TOKEN.equals(token)){
            ApiResult<String> ret1 = new ApiResult<String>();
            ret1.setData("");
            ret1.setRemark("Token is invalid!");
            ret1.setResult(ApiResult.RET_INVALID_TOKEN);
            return ret1;
        }
        ApiResult<String> ret = new ApiResult<String>();
        try
        {
            this.mqttProxyService.reInitService();
            ret.setRemark("reinitmqttproxyserivce successfully");   
            ret.setResult(ApiResult.RET_SUCC);                       
        }catch(Exception e){
            ret.setRemark(e.getMessage());
            ret.setResult(ApiResult.RET_FAIL);              
            logger.error("reinitmqttproxyserivce failed!", e);
        }
        return ret;
    }
    
}
